package com.anyisn.flickr.as3.patterns.proxy
{
	import com.adobe.webapis.flickr.Photo;
	import com.adobe.webapis.flickr.events.FlickrResultEvent;
	import com.elad.framework.utils.DownloadManager;
	
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	
	import com.anyisn.anyflickr.shell.ShellFacade;
	import com.anyisn.flickr.as3.utils.Urls;
	
	public class FlickrPhotoProxy extends FlickrServiceProxy
	{
		public var currentPhoto:Photo;
        protected var _loader:Loader;
        protected var _loading:Boolean;
		
		public function FlickrPhotoProxy(proxyName:String=null, data:Object=null)
		{
			super(proxyName, data);
		}
		
//		LOAD
		public function load(photo:Photo):void {
			currentPhoto = photo;
			_loader = new Loader();
			_loader.contentLoaderInfo.addEventListener(Event.OPEN, openLoad);
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeLoad);
			_loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressLoad);
			_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
			var request:URLRequest = new URLRequest();
//			var imageURL:String = "http://static.flickr.com/" + photo.server + "/" + photo.id + "_" + photo.secret + ".jpg";
			request.url = Urls.getPhotoSrc(photo);
			_loader.load(request);
			_loading = true;
		}
		
		public function cancelLoad():void {
			_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, completeLoad);
			_loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, progressLoad);
			_loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onIOError);
			_loader.close();
			_loader.unload();
			_loading = false;
			sendNotification(ShellFacade.PHOTO_LOAD_CANCELED)
		}

//		DOWNLOAD
		public function downloadFile(url:String, filePath:String):void {
			var downloadHelper:DownloadManager = new DownloadManager();
			
			downloadHelper.addEventListener(ProgressEvent.PROGRESS, onDownloadProgress);
			downloadHelper.addEventListener(Event.COMPLETE, onDownloadComplete);
			downloadHelper.downloadFileFromServer(url, filePath);
		}
		
		protected function onDownloadProgress(event:ProgressEvent):void {
			sendNotification(ShellFacade.PHOTO_DOWNLOAD_PROGRESS,[event.bytesLoaded, event.bytesTotal]);
		}
		
		protected function onDownloadComplete(event:Event):void {
			sendNotification(ShellFacade.PHOTO_DOWNLOAD_COMPLETE);
		}

//		------------------------------------------------		
		private function openLoad(e:Event):void {
			_loader.contentLoaderInfo.removeEventListener(Event.OPEN, openLoad);
			sendNotification(ShellFacade.PHOTO_LOAD_OPEN);
		}
		
		protected function completeLoad(e:Event):void {
			_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, completeLoad);
			_loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, progressLoad);
			_loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onIOError);
			_loading = false;
			var bitmap:Bitmap = _loader.content as Bitmap;
			_loader.unload();
			sendNotification(ShellFacade.PHOTO_LOAD_COMPLETE, [bitmap, currentPhoto]);
		}
		
		private function progressLoad(e:ProgressEvent):void {	
			sendNotification(ShellFacade.PHOTO_LOAD_PROGRESS, [e.bytesLoaded, e.bytesTotal]);
		}
		
		public function getSizes(photo_id:String):void {
			service.addEventListener(FlickrResultEvent.PHOTOS_GET_SIZES, onPhotosGetSizes);
			service.photos.getSizes(photo_id);
		}
		
		protected function onPhotosGetSizes(event:FlickrResultEvent):void {
			service.removeEventListener(FlickrResultEvent.PHOTOS_GET_SIZES, onPhotosGetSizes);
			if(event.success) {
				sendNotification(ShellFacade.PHOTOS_GET_SIZES_SUCCESS, event.data.photoSizes);
			} else {
				onFlickrError(event);
				sendNotification(ShellFacade.PHOTOS_GET_SIZES_FAILED);
			}
		}
		
		// favorites
		public function addToFavorites(photo:Photo):void {
			service.addEventListener(FlickrResultEvent.FAVORITES_ADD, onAddToFavorites);
			service.favorites.add(photo.id);
		}
		
		private function onAddToFavorites(event:FlickrResultEvent):void {
			service.removeEventListener(FlickrResultEvent.FAVORITES_ADD, onAddToFavorites);
			if(event.success) {
				sendNotification(ShellFacade.FAVORITES_ADD_SUCCESS);
			} else {
				onFlickrError(event);
				sendNotification(ShellFacade.FAVORITES_ADD_FAILED);
			}
		}
		
		public function removeFromFavorites(photo:Photo):void {
			service.addEventListener(FlickrResultEvent.FAVORITES_REMOVE, onRemoveFromFavorites);
			service.favorites.remove(photo.id);
		}
		
		public function onRemoveFromFavorites(event:FlickrResultEvent):void {
			service.removeEventListener(FlickrResultEvent.FAVORITES_REMOVE, onRemoveFromFavorites);
			if(event.success) {
				sendNotification(ShellFacade.FAVORITES_REMOVE_SUCCESS);
			} else {
				onFlickrError(event);
				sendNotification(ShellFacade.FAVORITES_REMOVE_FAILED);
			}
		}
	}
}